<?php

namespace App\Providers;

use Illuminate\Support\ServiceProvider;
use Illuminate\Support\Facades\View;
use Spatie\Permission\Models\Permission;


class AppServiceProvider extends ServiceProvider
{
    /**
     * Bootstrap any application services.
     *
     * @return void
     */
    public function boot()
    {
            $permission = Permission::where(['ismenu'=>1,'status'=>1, 'parentid'=>0])->orderBy('weight','desc')->get()->keyBy('id')->toArray();
            $data = array();
            foreach ($permission as $k=>$v){
                if('/'.\Request::path() == $v['rule'] || ( '/'.\Request::path() == '/admin/index' && $v['rule'] == '/admin/main' )  ){ $v['isselect'] = 1;}
                $data[$v['id']] = $v;
                $child = Permission::where(['ismenu'=>1,'status'=>1, 'parentid'=>$v['id']])->orderBy('weight','desc')->get()->keyBy('id')->toArray();
                if(count($child)){
                    $data[$v['id']]['child'] = $child;
                    foreach ($child as $key => $value) {
                        if('/'.\Request::path() == $value['rule']){$data[$value['parentid']]['isselect'] = 1;}
                    }
                }
            }
        View::share(['menu'=>$data]);
    }

    /**
     * Register any application services.
     *
     * @return void
     */
    public function register()
    {
        //
    }
}
